package com.easy.modules.sysmgr.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpSession;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.easy.common.dao.IBaseDao;
import com.easy.common.util.BeanUtils;
import com.easy.common.util.Const;
import com.easy.common.util.date.DateUtils;
import com.easy.common.web.page.DataGrid;
import com.easy.common.web.page.Msg;
import com.easy.common.web.page.Pager;
import com.easy.common.web.servlet.ApplicationConstants;
import com.easy.modules.sysmgr.entity.CompanyEntity;
import com.easy.modules.sysmgr.entity.DeptEntity;
import com.easy.modules.sysmgr.entity.MenuEntity;
import com.easy.modules.sysmgr.entity.PermitsEntity;
import com.easy.modules.sysmgr.entity.UserEntity;
import com.easy.modules.sysmgr.entity.UserRoleEntity;
import com.easy.modules.sysmgr.entity.UserWorkGroupEntity;
import com.easy.modules.sysmgr.service.UserServiceI;
import com.easy.modules.sysmgr.web.form.UserForm;
import com.easy.modules.sysmgr.web.form.UserRoleForm;
import com.easy.modules.sysmgr.web.form.UserSession;
import com.easy.modules.sysmgr.web.form.UserWorkGroupForm;

@Service
@Transactional
public class UserServiceImpl implements UserServiceI {

	@Autowired
	private IBaseDao<UserEntity> basedaoUser;
	
	@Autowired
	private IBaseDao<DeptEntity> basedaoDept;
	
	@Autowired
	private IBaseDao<UserWorkGroupEntity> basedaoGroup;
	
	@Autowired
	private IBaseDao<CompanyEntity> basedaoCompany;
	
	@Autowired
	private IBaseDao<UserRoleEntity> basedaoUserRole;
	
	@Autowired
	private IBaseDao<PermitsEntity> basedaoPermits;
	
	@Override
	public Msg save(UserForm form) {
		try {
			UserEntity entity = new UserEntity();
			BeanUtils.copyNotNullProperties(form, entity);
			entity.setCreated(new Date()) ; 
			
			//密码设为空字符串
			if(null == entity.getPassword()) {
				entity.setPassword("") ;
			}
			
			if(null != form.getCompany_id()) {
				entity.setCompany(this.basedaoCompany.load(CompanyEntity.class, form.getCompany_id())) ;
			}
			if(null != form.getDept_id()) {
				entity.setDept(this.basedaoDept.load(DeptEntity.class, form.getDept_id()));
			}
			if(null != form.getWorkgroup_ids() && !"".equals(form.getWorkgroup_ids())) {
				Set<UserWorkGroupEntity> set = new HashSet<UserWorkGroupEntity>() ;
				String[] split = form.getWorkgroup_ids().split(",") ;
				for (String group_id : split) {
					set.add(this.basedaoGroup.load(UserWorkGroupEntity.class, group_id)) ;
				}
				entity.setWorkgroups(set) ;
			}
			
			if(null != form.getUserrole_ids() && !"".equals(form.getUserrole_ids())) {
				Set<UserRoleEntity> set = new HashSet<UserRoleEntity>() ;
				String[] split = form.getUserrole_ids().split(",") ;
				for (String userrole_id : split) {
					set.add(this.basedaoUserRole.load(UserRoleEntity.class, userrole_id)) ;
				}
				entity.setUserroles(set) ;
			}
			
			this.basedaoUser.add(entity);
			
			return new Msg(true, "创建成功！") ;
		} catch (BeansException e) {
			return new Msg(false, "创建失败！") ;
		}
	}
	
	@Override
	public Msg delete(UserForm form) {
		try {
			if(null != form.getIds() && !"".equals(form.getIds())) {
				String[] ids = form.getIds().split(",") ;
				for (String id : ids) {
					this.basedaoUser.delete(UserEntity.class, id) ;
				}
				return new Msg(true, "删除成功！") ;
			} else {
				return new Msg(false, "删除失败！") ;
			}
		} catch (BeansException e) {
			return new Msg(false, "删除失败！") ;
		}
	}
	
	@Override
	public UserForm get(UserForm form) {
		Map<String, Object> params = new HashMap<String, Object>();
		String hql = "select t from UserEntity t where 1=1";
		hql = addWhere(hql, form, params) ;
		
		UserEntity entity = (UserEntity) this.basedaoUser.queryObject(hql, params) ;
		if(null != entity) {
			UserForm pform = new UserForm();
			BeanUtils.copyNotNullProperties(entity, pform);
			
			CompanyEntity company = entity.getCompany() ;
			if (company != null) {
				pform.setCompany_id(company.getId()) ;
				pform.setCompany_name(company.getName()) ;
			}
			
			DeptEntity dept = entity.getDept() ;
			if (dept != null) {
				pform.setDept_id(dept.getId()) ;
				pform.setDept_name(dept.getName()) ;
			}
			
			Set<UserWorkGroupEntity> workgroups = entity.getWorkgroups() ;
			if (workgroups != null && !workgroups.isEmpty()) {
				StringBuffer workgroupIds = new StringBuffer() ;
				StringBuffer workgroupNames = new StringBuffer() ;
				for (UserWorkGroupEntity workgroup : workgroups) {
					workgroupIds.append(workgroup.getId()).append(",") ;
					workgroupNames.append(workgroup.getName()).append(",") ;
				}
				pform.setWorkgroup_ids(workgroupIds.deleteCharAt(workgroupIds.length()-1).toString()) ;
				pform.setWorkgroup_names(workgroupNames.deleteCharAt(workgroupNames.length()-1).toString()) ;
			}
			
			Set<UserRoleEntity> userroles = entity.getUserroles() ;
			if (userroles != null && !userroles.isEmpty()) {
				StringBuffer userroleIds = new StringBuffer() ;
				StringBuffer userroleNames = new StringBuffer() ;
				for (UserRoleEntity userrole : userroles) {
					userroleIds.append(userrole.getId()).append(",") ;
					userroleNames.append(userrole.getName()).append(",") ;
				}
				pform.setUserrole_ids(userroleIds.deleteCharAt(userroleIds.length()-1).toString()) ;
				pform.setUserrole_names(userroleNames.deleteCharAt(userroleNames.length()-1).toString()) ;
			}
			
			Set<PermitsEntity> permits = entity.getPermits() ;
			if (permits != null && !permits.isEmpty()) {
				StringBuffer permitsIds = new StringBuffer() ;
				StringBuffer permitsNames = new StringBuffer() ;
				for (PermitsEntity permit : permits) {
					permitsIds.append(permit.getId()).append(",") ;
					permitsNames.append(permit.getName()).append(",") ;
				}
				pform.setPermits_ids(permitsIds.deleteCharAt(permitsIds.length()-1).toString()) ;
				pform.setPermits_names(permitsNames.deleteCharAt(permitsNames.length()-1).toString()) ;
			}
			
			
			return pform;
		} else {
			return null ;
		}
	}
	
	@Override
	public Msg edit(UserForm form) {
		try {
			UserEntity entity = this.basedaoUser.load(UserEntity.class, form.getId());
			
			if(entity != null) {
				BeanUtils.copyNotNullProperties(form, entity, new String[]{"password"});
				entity.setModifyDate(new Date()) ;
				
				if(null != form.getCompany_id()) {
					entity.setCompany(this.basedaoCompany.load(CompanyEntity.class, form.getCompany_id())) ;
				} else {
					entity.setCompany(null);
				}
				if (form.getDept_id() != null) {
					entity.setDept(this.basedaoDept.load(DeptEntity.class, form.getDept_id()));
				} else {
					entity.setDept(null);
				}
				
				if(null != form.getWorkgroup_ids() && !"".equals(form.getWorkgroup_ids())) {
					Set<UserWorkGroupEntity> set = new HashSet<UserWorkGroupEntity>() ;
					String[] split = form.getWorkgroup_ids().split(",") ;
					for (String group_id : split) {
						set.add(this.basedaoGroup.load(UserWorkGroupEntity.class, group_id)) ;
					}
					entity.setWorkgroups(set) ;
				} else {
					entity.setWorkgroups(null) ;
				}
				
				if(null != form.getUserrole_ids() && !"".equals(form.getUserrole_ids())) {
					Set<UserRoleEntity> set = new HashSet<UserRoleEntity>() ;
					String[] split = form.getUserrole_ids().split(",") ;
					for (String userrole_id : split) {
						set.add(this.basedaoUserRole.load(UserRoleEntity.class, userrole_id)) ;
					}
					entity.setUserroles(set) ;
				} else {
					entity.setUserroles(null) ;
				}
				
				if(null != form.getPermits_ids() && !"".equals(form.getPermits_ids())) {
					Set<PermitsEntity> set = new HashSet<PermitsEntity>() ;
					String[] split = form.getPermits_ids().split(",") ;
					for (String userrole_id : split) {
						set.add(this.basedaoPermits.load(PermitsEntity.class, userrole_id)) ;
					}
					entity.setPermits(set) ;
				} else {
					entity.setPermits(null) ;
				}
				
				this.basedaoUser.update(entity) ;
			}
			
			return new Msg(true, "编辑成功！") ;
		} catch (BeansException e) {
			return new Msg(false, "编辑失败！") ;
		}
	}

	@Override
	public DataGrid datagrid(UserForm form) { 
		
		List<UserForm> forms = new ArrayList<UserForm>();
		Pager<UserEntity> pager = this.find(form) ;
		if (null != pager && !pager.getDataRows().isEmpty()) {
			for (UserEntity user : pager.getDataRows()) {
				UserForm uf = new UserForm();
				BeanUtils.copyNotNullProperties(user, uf);
				
				Set<UserWorkGroupEntity> workgroups = user.getWorkgroups() ;
				if(null != workgroups && !workgroups.isEmpty()) {
					StringBuffer groupIds = new StringBuffer() ;
					StringBuffer groupNames = new StringBuffer() ;
					for (UserWorkGroupEntity group : workgroups) {
						groupIds.append(group.getId()).append(",") ;
						groupNames.append(group.getName()).append(",") ;
					}
					uf.setWorkgroup_ids(groupIds.deleteCharAt(groupIds.length()-1).toString()) ;
					uf.setWorkgroup_names(groupNames.deleteCharAt(groupNames.length()-1).toString()) ;
				}
				
				Set<UserRoleEntity> userroles = user.getUserroles() ;
				if(null != userroles && !userroles.isEmpty()) {
					StringBuffer userroleIds = new StringBuffer() ;
					StringBuffer userroleNames = new StringBuffer() ;
					for (UserRoleEntity userrole : userroles) {
						userroleIds.append(userrole.getId()).append(",") ;
						userroleNames.append(userrole.getName()).append(",") ;
					}
					uf.setUserrole_ids(userroleIds.deleteCharAt(userroleIds.length()-1).toString()) ;
					uf.setUserrole_names(userroleNames.deleteCharAt(userroleNames.length()-1).toString()) ;
				}
				
				Set<PermitsEntity> permits = user.getPermits() ;
				if(null != permits && !permits.isEmpty()) {
					StringBuffer permitsIds = new StringBuffer() ;
					StringBuffer permitsNames = new StringBuffer() ;
					for (PermitsEntity permit : permits) {
						permitsIds.append(permit.getId()).append(",") ;
						permitsNames.append(permit.getName()).append(",") ;
					}
					uf.setPermits_ids(permitsIds.deleteCharAt(permitsIds.length()-1).toString()) ;
					uf.setPermits_names(permitsNames.deleteCharAt(permitsNames.length()-1).toString()) ;
				}
				
				if(null != user.getCompany()) {
					uf.setCompany_id(user.getCompany().getId()) ;
					uf.setCompany_name(user.getCompany().getName()) ;
				}
				
				if(null != user.getDept()) {
					uf.setDept_id(user.getDept().getId()) ;
					uf.setDept_name(user.getDept().getName()) ;
				}
				uf.setPassword((uf.getPassword()!=null && !"".equals(uf.getPassword().trim())?"******":"")) ;
				
				if(null == uf.getLastAcceccTime() || "".equals(uf.getLastAcceccTime())) {
					uf.setLastAcceccTime("") ;
					uf.setDiffDatetime("未曾登录") ;
				} else {
					//计算离线时间
					Set<String> keySet = ApplicationConstants.SESSION_MAP.keySet() ;
					if(null != keySet && !keySet.isEmpty()) {
						for(String id : keySet){
							HttpSession sess = ApplicationConstants.SESSION_MAP.get(id);
							UserSession userSession = (UserSession)sess.getAttribute(Const.USER_SESSION);
							if(null != userSession) {
								if(uf.getId().equalsIgnoreCase(userSession.getId())) {
									uf.setDiffDatetime("0天 0时 0分 0秒") ;
								} else {
									uf.setDiffDatetime(DateUtils.dateDiffStr(uf.getLastAcceccTime(), DateUtils.getSysDateTime(), DateUtils.FORMAT_DATETIME)) ;
								}
							}
						}
					} else {
						uf.setDiffDatetime(DateUtils.dateDiffStr(uf.getLastAcceccTime(), DateUtils.getSysDateTime(), DateUtils.FORMAT_DATETIME)) ;
					}
				}
				forms.add(uf);
			}
		}
		
		DataGrid datagrid = new DataGrid();
		datagrid.setTotal(pager.getTotal());
		datagrid.setRows(forms);
		return datagrid;
	}


	private Pager<UserEntity> find(UserForm form) {
		Map<String, Object> params = new HashMap<String, Object>();
		String hql = "select t from UserEntity t where 1=1";
		hql = addWhere(hql, form, params) ;
		return this.basedaoUser.find(hql, params);
	}

	private String addWhere(String hql, UserForm form, Map<String, Object> params) {
		if (null != form) {
			if (form.getId() != null && !"".equals(form.getId())) {
				hql += " and t.id=:id";
				params.put("id", form.getId());
			}
			if (form.getNum() != null && !"".equals(form.getNum())) {
				hql += " and t.num like :num";
				params.put("num", "%%" + form.getNum() + "%%");
			}
			if (form.getAccount() != null && !"".equals(form.getAccount())) {
				hql += " and t.account like :account";
				params.put("account", "%%" + form.getAccount() + "%%");
			}
			if (form.getTruename() != null && !"".equals(form.getTruename())) {
				hql += " and t.truename like :truename";
				params.put("truename", "%%" + form.getTruename() + "%%");
			}
			if (form.getSex() != null && !"".equals(form.getSex())) {
				hql += " and t.sex like :sex";
				params.put("sex", "%%" + form.getSex() + "%%");
			}
			if (form.getStartDate() != null && !"".equals(form.getStartDate())) {
				hql += " and t.created>= :startDate ";
				params.put("startDate", form.getStartDate());
			}
			if (form.getEndDate() != null && !"".equals(form.getEndDate())) {
				hql += " and t.created<= :endDate ";
				params.put("endDate", form.getEndDate());
			}
			if (form.getCompany_id() != null && !"".equals(form.getCompany_id())) {
				hql += " and t.company.id= :company_id ";
				params.put("company_id", form.getCompany_id());
			}
			if (form.getDept_id() != null && !"".equals(form.getDept_id())) {
				hql += " and t.dept.id= :dept_id ";
				params.put("dept_id", form.getDept_id());
			}
		}
		return hql;
	}

	@Override
	public Msg batchUserWorkGroup(UserForm form) {
		try {
			if(null != form.getIds() && !"".equals(form.getIds())) {
				String[] ids = form.getIds().split(",") ;
				for (String id : ids) {
					UserEntity user = this.basedaoUser.load(UserEntity.class, id);
					
					if(null != form.getWorkgroup_ids() && !"".equals(form.getWorkgroup_ids())) {
						Set<UserWorkGroupEntity> set = new HashSet<UserWorkGroupEntity>() ;
						String[] split = form.getWorkgroup_ids().split(",") ;
						for (String group_id : split) {
							set.add(this.basedaoGroup.load(UserWorkGroupEntity.class, group_id)) ;
						}
						user.setWorkgroups(set) ;
					} else {
						user.setWorkgroups(null) ;
					}
					
					user.setModifyDate(new Date()) ;
				}
				return new Msg(true, "批量设置用户组成功！") ;
			} else {
				return new Msg(false, "批量设置用户组失败！") ;
			}
		} catch (BeansException e) {
			return new Msg(false, "程序报错！") ;
		}
	}

	@Override
	public Msg batchDept(UserForm form) {
		try {
			if(null != form.getIds() && !"".equals(form.getIds())) {
				String[] ids = form.getIds().split(",") ;
				
				CompanyEntity company = this.basedaoCompany.load(CompanyEntity.class, form.getCompany_id()) ;
				DeptEntity dept = this.basedaoDept.load(DeptEntity.class, form.getDept_id()) ;
				
				for (String id : ids) {
					UserEntity user = this.basedaoUser.load(UserEntity.class, id);
					
					user.setCompany(company) ;
					user.setDept(dept) ;
					user.setModifyDate(new Date()) ;
					this.basedaoUser.update(user) ;
				}
				return new Msg(true, "批量设置部门成功！") ;
			} else {
				return new Msg(false, "批量设置部门失败！") ;
			}
		} catch (BeansException e) {
			return new Msg(false, "批量设置部门失败！") ;
		}
	}

	@Override
	public Msg batchUserRole(UserForm form) {
		try {
			if(null != form.getIds() && !"".equals(form.getIds())) {
				
				Set<UserRoleEntity> roleSet = new HashSet<UserRoleEntity>() ;
				if(null != form.getUserrole_ids() && !"".equals(form.getUserrole_ids())) {
					String[] ids = form.getUserrole_ids().split(",") ;
					for (String id : ids) {
						roleSet.add(this.basedaoUserRole.load(UserRoleEntity.class, id)) ;
					}
				}
				
				String[] ids = form.getIds().split(",") ;
				for (String id : ids) {
					UserEntity user = this.basedaoUser.load(UserEntity.class, id);
					user.setUserroles(roleSet) ;
 					user.setModifyDate(new Date()) ;
					this.basedaoUser.update(user) ;
				}
				return new Msg(true, "加入角色成功！") ;
			} else {
				return new Msg(false, "加入角色失败！") ;
			}
		} catch (BeansException e) {
			return new Msg(false, "批量加入角色失败！") ;
		}
	}
	
	@Override
	public Msg batchPermits(UserForm form) {
		try {
			if(null != form.getIds() && !"".equals(form.getIds())) {
				
				Set<PermitsEntity> permitSet = new HashSet<PermitsEntity>() ;
				if(null != form.getPermits_ids() && !"".equals(form.getPermits_ids())) {
					String[] ids = form.getPermits_ids().split(",") ;
					for (String id : ids) {
						permitSet.add(this.basedaoPermits.load(PermitsEntity.class, id)) ;
					}
				}
				
				String[] ids = form.getIds().split(",") ;
				for (String id : ids) {
					UserEntity user = this.basedaoUser.load(UserEntity.class, id);
					user.setPermits(permitSet) ;
					user.setModifyDate(new Date()) ;
					this.basedaoUser.update(user) ;
				}
				return new Msg(true, "批量加入权限组成功！") ;
			} else {
				return new Msg(false, "批量加入权限组失败！") ;
			}
		} catch (BeansException e) {
			return new Msg(false, "批量加入权限组失败！") ;
		}
	}

	@Override
	public Msg clearPwd(UserForm form) {
		try {
			if(null != form.getIds() && !"".equals(form.getIds())) {
				String[] ids = form.getIds().split(",") ;
				for (String id : ids) {
					UserEntity user = this.basedaoUser.load(UserEntity.class, id);
					user.setPassword("") ;
					user.setModifyDate(new Date()) ;
					this.basedaoUser.update(user) ;
				}
				return new Msg(true, "清除密码成功！") ;
			} else {
				return new Msg(false, "清除密码失败！") ;
			}
		} catch (BeansException e) {
			return new Msg(false, "清除密码失败！") ;
		}
	}

	@Override
	public Msg resetPwd(UserForm form) {
		try {
			if(null != form.getIds() && !"".equals(form.getIds())) {
				String[] ids = form.getIds().split(",") ;
				for (String id : ids) {
					UserEntity user = this.basedaoUser.load(UserEntity.class, id);
					user.setPassword(form.getPassword()) ;
					user.setModifyDate(new Date()) ;
					this.basedaoUser.update(user) ;
				}
				return new Msg(true, "重设密码成功！") ;
			} else {
				return new Msg(false, "重设密码失败！") ;
			}
		} catch (BeansException e) {
			return new Msg(false, "重设密码失败！") ;
		}
	}

	@Override
	public Msg lockAccount(UserForm form) {
		try {
			if(null != form.getIds() && !"".equals(form.getIds())) {
				String[] ids = form.getIds().split(",") ;
				for (String id : ids) {
					UserEntity user = this.basedaoUser.load(UserEntity.class, id);
					user.setStatus(form.getStatus()) ;
					user.setModifyDate(new Date()) ;
					this.basedaoUser.update(user) ;
				}
			}
		} catch (BeansException e) {
			return new Msg(false, "程序发送错误！") ;
		}
		
		if(form.getStatus() == 0) {
			return new Msg(true, "账号已解锁！") ;
		} else if(form.getStatus() == 1) {
			return new Msg(true, "账号已解锁！") ;
		} else {
			return new Msg(true, "出现错误！") ;
		}
	}

	@Override
	public UserForm login(UserForm form) {
		Map<String, Object> params = new HashMap<String, Object>() ;
		params.put("account", form.getAccount()) ;
		params.put("pwd", form.getPassword()) ;
		UserEntity entity = (UserEntity) this.basedaoUser.queryObject("select t from UserEntity t where t.account=:account and t.password=:pwd", params) ;
		if(null != entity) {
			entity.setLastAcceccTime(DateUtils.getSysDateTime()) ;
			UserForm f = new UserForm() ;
			BeanUtils.copyNotNullProperties(entity, f) ;
			return f ;
		} else {
			return null ;
		}
	}

	@Override
	public Set<String> getMyAuths(UserForm form) {
		Set<String> auths = new HashSet<String> () ;
		
		UserEntity user = this.basedaoUser.load(UserEntity.class, form.getId()) ;
		
		Set<UserWorkGroupEntity> workgroups = user.getWorkgroups() ;
		Set<UserRoleEntity> userroles = user.getUserroles() ;
		Set<PermitsEntity> permits = user.getPermits() ;
		
		if(null != workgroups && !workgroups.isEmpty()) {
			for (UserWorkGroupEntity group : workgroups) {
				Set<PermitsEntity> group_roles = group.getPermits() ;
				for (PermitsEntity r : group_roles) {
					Set<MenuEntity> menus = r.getMenus() ;
					for (MenuEntity m : menus) {
						auths.add(m.getHref()) ;
					}
				}
			}
			
		}
		
		if(null != userroles && !userroles.isEmpty()) {
			for (UserRoleEntity userorle : userroles) {
				Set<PermitsEntity> urs = userorle.getPermits() ;
				for (PermitsEntity r : urs) {
					Set<MenuEntity> menus = r.getMenus() ;
					for (MenuEntity m : menus) {
						auths.add(m.getHref()) ;
					}
				}
			}
			
		}
		
		if(null != permits && !permits.isEmpty()) {
			for (PermitsEntity r : permits) {
				Set<MenuEntity> menus = r.getMenus() ;
				for (MenuEntity m : menus) {
					auths.add(m.getHref()) ;
				}
			}
		}
		return auths;
	}

	@Override
	public Msg modifyPwd(UserForm form) {
		UserEntity entity = this.basedaoUser.load(UserEntity.class, form.getId()) ;
		
		if(entity != null) {
			if(form.getOldPwd().equals(entity.getPassword())) {
				entity.setPassword(form.getPassword()) ;
				return new Msg(true, "密码修改成功，重新登录生效。") ;
			} else {
				return new Msg(false, "原密码不正确，请重新输入！") ;
			}
		} else {
			return new Msg(false, "程序错误！") ;
		}
	}

	@Override
	public Msg MyAccountInfo(UserForm form) {
		Map<String, Object> map = new HashMap<String, Object>() ;
		
		UserEntity user = this.basedaoUser.load(UserEntity.class, form.getId()) ;
		BeanUtils.copyNotNullProperties(user, form) ;
		
		map.put("user", form) ;
		
		List<UserWorkGroupForm> workgroupList = new ArrayList<UserWorkGroupForm>() ;
		Set<UserWorkGroupEntity> workgroups = user.getWorkgroups() ;
		if(null != workgroups && !workgroups.isEmpty()) {
			for (UserWorkGroupEntity g : workgroups) {
				UserWorkGroupForm gf = new UserWorkGroupForm() ;
				BeanUtils.copyNotNullProperties(g, gf) ;
				workgroupList.add(gf) ;
			}
			map.put("workgroups", workgroupList) ;
		}
		
		List<UserRoleForm> userroleList = new ArrayList<UserRoleForm>() ;
		Set<UserRoleEntity> userorles = user.getUserroles() ;
		if(null != userorles && !userorles.isEmpty()) {
			for (UserRoleEntity g : userorles) {
				UserRoleForm urf = new UserRoleForm() ;
				BeanUtils.copyNotNullProperties(g, urf) ;
				userroleList.add(urf) ;
			}
			map.put("userroles", userroleList) ;
		}
		
		List<UserRoleForm> roleList = new ArrayList<UserRoleForm>() ;
		Set<PermitsEntity> roles = user.getPermits() ;
		if(null != roles && !roles.isEmpty()) {
			for (PermitsEntity r : roles) {
				UserRoleForm rf = new UserRoleForm() ;
				BeanUtils.copyNotNullProperties(r, rf) ;
				roleList.add(rf) ;
			}
			map.put("permits", roleList) ;
		}
		
		return new Msg(true, null, map);
	}
}
